package com.dycc.libary.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dycc.libary.R;
import com.dycc.libary.util.ViewColor;
import com.dycc.libary.util.windowstatus.StatusBarUtil;

import butterknife.ButterKnife;

/**
 * fragment基类
 * Created by 吴学坤 on 2017/9/17.
 */
public abstract class BaseFragment extends Fragment {
    private View view;
    protected int statusBarHeight;

    protected abstract int getLayoutId();

    protected abstract void initView(View view, Bundle savedInstanceState);

    protected abstract void initData();

    protected void releaseView() {

    }

    public void setWindowStatu(View topView) {
        if (StatusBarUtil.StatusBarLightMode(getActivity()) == 0) {
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            statusBarHeight = ViewColor.getStatusBarHeight(getActivity());
            ViewGroup.LayoutParams params = topView.getLayoutParams();
            if (params == null || topView == null) return;
            params.width = screenWidth;
            params.height = statusBarHeight;
            topView.setLayoutParams(params);
            topView.requestLayout();
            topView.setBackgroundColor(getResources()
                    .getColor(R.color.statebar_blue));
        }
    }

    public void setWindowStatu(View topView, int bgColor) {
        if (StatusBarUtil.StatusBarLightMode(getActivity()) == 0) return;
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int statusBarHeight = ViewColor.getStatusBarHeight(getActivity());
        ViewGroup.LayoutParams params = topView.getLayoutParams();
        if (params == null || topView == null) return;
        params.width = screenWidth;
        params.height = statusBarHeight;
        topView.setLayoutParams(params);
        topView.requestLayout();
        try {
            topView.setBackgroundColor(getResources()
                    .getColor(bgColor));
        } catch (Exception ex) {
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(getLayoutId(), container, false);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        addStatusBar();
        initView(view, savedInstanceState);
        initData();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        releaseView();
    }


}
